<style lang="less">
</style>
<template>
  <div class="search">
    <Row>
      <Card>
        <p slot="title">编辑</p>
        <Form
          ref="form"
          :model="form"
          :label-width="90"
          :rules="formValidate"
          style="position:relative"
        >
          <Form-item label="协议内容" prop="content">
            <editor id="editor" v-model="form.content" ref="editor"></editor>
          </Form-item>
          <Form-item>
            <Button
              @click="handleSubmit"
              :loading="submitLoading"
              type="primary"
              style="margin-right:5px"
            >提交并保存</Button>
            <Button @click="closeCurrentPage">返回</Button>
          </Form-item>
          <Spin size="large" fix v-if="loading"></Spin>
        </Form>
      </Card>
    </Row>
  </div>
</template>

<script>
import editor from "@/views/my-components/xboot/editor";
export default {
  name: "edit",
  components: {
    editor
  },
  data() {
    return {
      loading: true, // 表单加载状态
      submitLoading: false, // 表单提交状态
      form: {
        id: "",
        content: ""
      },
      // 表单验证规则
      formValidate: {
        content: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      backRoute: ""
    };
  },
  methods: {
    init() {
      this.handleReset();
      this.form.id = this.$route.query.id;
      this.backRoute = this.$route.query.backRoute;
      this.getData();
    },
    handleReset() {
      this.$refs.form.resetFields();
    },
    getData() {
      this.loading = true;
      this.postRequest("/treaty/get/" + this.form.id).then(res => {
        this.loading = false;
        if (res.success) {
          // 转换null为""
          this.form.content = res.result.content;
          //   this.form.newsContent = res.result.newsContent;
          this.$refs.editor.setData(res.result.content);
          //   for (let attr in v) {
          //     if (v[attr] == null) {
          //       v[attr] = "";
          //     }
          //   }
          //   let str = JSON.stringify(v);
          //   let data = JSON.parse(str);
          //   this.form.newsContent = data;
        }
      });
    },
    handleSubmit() {
      this.submitLoading = true;
      this.$refs.form.validate(valid => {
        if (valid) {
          this.postRequest("/treaty/update", this.form).then(res => {
            this.submitLoading = false;
            if (res.success) {
              this.$Message.success("编辑成功");
              this.closeCurrentPage();
            }
          });
        }
      });
    },
    // 关闭当前页面
    closeCurrentPage() {
      this.$store.commit("removeTag", "edit");
      localStorage.pageOpenedList = JSON.stringify(
        this.$store.state.app.pageOpenedList
      );
      this.$router.push({
        name: this.backRoute
      });
    }
  },
  watch: {
    // 监听路由变化通过id获取数据
    $route(to, from) {
      if (to.name == "newsedit") {
        this.handleReset();
        this.form.id = this.$route.query.id;
        this.getData();
      }
    }
  },
  mounted() {
    this.init();
  }
};
</script>